package org.autumn.aqs;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        // 可以用于等待所有线程执行完毕,也可以用join实现
        CountDownLatch latch = new CountDownLatch(3);
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                System.out.println("start");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end");
                latch.countDown();
            }, "" + i).start();
        }

        System.out.println("main waiting...");
        latch.await();

    }
}
